package com.xs.controller;

import com.xs.common.JsonResult;
import com.xs.entity.Collection;
import com.xs.entity.Comments;
import com.xs.entity.utils.CountAndDayCount;
import com.xs.service.CollectionSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    private CollectionSevice collectionSevice;

    /**
     * 新增收藏
     */
    @PostMapping
    public JsonResult<?> add(@RequestBody Collection collection){
        collectionSevice.save(collection);
        return JsonResult.success();
    }

    /**
     * 取消收藏
     * @return
     */
    @DeleteMapping("/del/{id}/{uid}")
    public JsonResult<?> del(@PathVariable Integer id,
                             @PathVariable Integer uid){
        collectionSevice.removeByUdAndArticleId(id,uid);
        return JsonResult.success();
    }

    @GetMapping("/aticleId/{uid}")
    public JsonResult<?> getarticleIdonByUid(@PathVariable Integer uid){
        return JsonResult.success(collectionSevice.getarticleIdonByUid(uid));
    }



    //获取收藏总数以及当日的收藏数
    @GetMapping("/count")
    public JsonResult<?> countUser() throws ParseException {
        //获取总数量
        List<Collection> row = collectionSevice.list();
        List<String> time=new ArrayList<>();
        //创建时间转换器
        DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
        for (Collection user : row) {
            String format = df.format(user.getTime());
            time.add(format);
        }
        System.out.println(time);
        Date date = new Date();
        //获取当前时间的年月日
        String data = df.format(date);
        int count=0;

        for (String s : time) {
            if (s.equals(data)){
                count=count+1;
            }
        }
        //将数据打包
        CountAndDayCount rows=new CountAndDayCount();
        rows.setCount(collectionSevice.count());
        rows.setDayCount(count);

        return JsonResult.success(rows);
    }

}
